package com.lx.controller;

import com.lx.bean.*;
import com.lx.mvc.ResponseBody;
import com.lx.service.ExpressService;
import com.lx.service.UserService;
import com.lx.util.DateFormatUtil;
import com.lx.util.JsonUtil;
import com.lx.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ExpressController
 * @Description:
 * @Date Created in  2021-07-08 7:45
 * @Modified By:
 * @Author: 零巽
 */
public class ExpressController {
    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        List<Map<String, Integer>> console = ExpressService.console();
        Message message = new Message();
        if (console.size() == 0) {
            message.setStatus(-1);
        } else {
            message.setStatus(0);
        }
        message.setData(console);
        String json = JsonUtil.toJSON(message);
        return json;
    }

    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
        //1、获取分页参数

        int offset =Integer.parseInt(httpServletRequest.getParameter("offset"));//起始索引值
        int pageNumber =Integer.parseInt(httpServletRequest.getParameter("pageNumber"));//每页数据量

        //2、进行查询
        List<Express> list = ExpressService.findAll(true, offset, pageNumber);
        List<BootstrapTableExpress> expressList = new ArrayList<>();
        for (Express e : list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"未取件":"已取件";
            String code = e.getCode() == null||e.getCode().length()==0 ? "已取件" : e.getCode();
            BootstrapTableExpress express = new BootstrapTableExpress(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());
            expressList.add(express);
        }
        List<Map<String, Integer>> console = ExpressService.console();
        Integer total = console.get(0).get("data1_size");
        //3、将集合封装为Bootstrap-table识别的格式
        ResultData<BootstrapTableExpress> data = new ResultData<>();
        data.setRows(expressList);
        data.setTotal(total);
        String json = JsonUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/express/add.do")
    public String add(HttpServletRequest req, HttpServletResponse resp) {
        //获取参数
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String sysPhone = UserUtil.getSysPhone(req.getSession());
        //封装为对象
        Express express = new Express(number,userName,userPhone,company,sysPhone);
        //调用Service层方法
        boolean add = ExpressService.add(express);
        //根据结果创建消息对象
        Message message = new Message();
        if (add) {
            User byPhone = UserService.findByPhone(userPhone);
            if (byPhone == null) {
                User user = new User();
                user.setUserName(userName);
                user.setPhone(userPhone);
                UserService.addUser(user);
            }
            message.setStatus(0);
            message.setResult("录入成功！");
        } else {
            message.setStatus(-1);
            message.setResult("录入失败！");
        }
        //将消息包装为json对象
        String json = JsonUtil.toJSON(message);
        //返回json对象
        return json;
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest req, HttpServletResponse response) {
        String number = req.getParameter("number");
        Express express = ExpressService.findByNumber(number);
        Message message = new Message();
        if (express == null) {
            message.setStatus(-1);
            message.setResult("輸入的单号不存在！");
        } else {
            message.setStatus(0);
            message.setResult("查询成功！");
            message.setData(express);
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest req,HttpServletResponse resp) {
        String id = req.getParameter("id");
        String number = req.getParameter("number");
        String company = req.getParameter("company");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String status = req.getParameter("status");
        System.out.println(status);
        System.out.println(userPhone);
        String sysPhone = UserUtil.getSysPhone(req.getSession());
        Express express = new Express(number,userName,userPhone,company,sysPhone);
        express.setStatus(Integer.parseInt(status));
        boolean update = ExpressService.update(Integer.parseInt(id), express);
        Message message = new Message();
        if (update) {
            message.setStatus(0);
            message.setResult("修改成功");
            User byPhone = UserService.findByPhone(userPhone);
            if (byPhone == null) {
                User user = new User();
                user.setUserName(userName);
                user.setPhone(userPhone);
                UserService.addUser(user);
            }
        } else {
            message.setStatus(-1);
            message.setResult("修改失败");
        }
        return JsonUtil.toJSON(message);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest req,HttpServletResponse resp) {
        String id = req.getParameter("id");
        boolean b = ExpressService.deleteById(Integer.parseInt(id));
        Message message = new Message();
        if (b) {
            message.setStatus(0);
            message.setResult("删除成功");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JsonUtil.toJSON(message);
    }
}
